package com.nanfang51g3.ckz.ui.fragment.home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.adapter.ext.AccountingAdapter;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.domain.AccountingBean;
import com.nanfang51g3.ckz.ui.accounting.AccountingContract;
import com.nanfang51g3.ckz.ui.accounting.AccountingPresenter;
import com.nanfang51g3.ckz.ui.accounting.AccountingPresenterModule;
import com.nanfang51g3.ckz.ui.accounting.DaggerAccountingContractComponent;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.fragment.base.BaseLazyLoadFragment;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by Administrator on 2016/5/18.
 */
public class HomeAccountingFragment extends BaseLazyLoadFragment implements AccountingContract.View {

    AccountingContract.AccountingActionsListener accountingActionsListener;
    AccountingAdapter accountingAdapter;
    ArrayList<AccountingBean> accountingBeens = new ArrayList<>();
    @Bind(R.id.accounting_recycler_view)
    RecyclerView accountingRecyclerView;
    @Bind(R.id.accounting_view_frame)
    PtrClassicFrameLayout accountingViewFrame;
    @Bind(R.id.home_tv_title)
    TextView homeTvTitle;
    int page = 1;
    int limit = 10;
    boolean more = false;

    @Override
    public IPresenter getPresenter() {
        return accountingActionsListener;
    }

    public static HomeAccountingFragment newInstance(Bundle args) {
        HomeAccountingFragment pageFragment = new HomeAccountingFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        homeTvTitle.setText(getResources().getString(R.string.str_accounting));
        KLog.i("Evd",  "onResume");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_accounting, container, false);
        ButterKnife.bind(this, view);
        accountingActionsListener = DaggerAccountingContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .accountingPresenterModule(new AccountingPresenterModule(this))
                .build().getAccountingPresenter();
        KLog.i("Evd", accountingActionsListener == null ? "onCreateView - ==null" : "onCreateView - !=null");
        AccountingBean accountingBean = new AccountingBean();
        accountingBean.setLayoutType(0);
        accountingBean.setAll_total("0");
        accountingBean.setDeal_total("0");
        accountingBeens.add(0, accountingBean);
        accountingViewFrame.setLastUpdateTimeRelateObject(this);

        accountingViewFrame.setPtrHandler(new PtrDefaultHandler2() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // TODO Auto-generated method stub
                more = false;
                page = 1;

                /**
                 * @see AccountingPresenter#adList(int, int)
                 */
                accountingActionsListener.adList(page, limit);
                accountingViewFrame.refreshComplete();
            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                // TODO Auto-generated method stub
                more = true;
                ++page;
                /**
                 * @see AccountingPresenter#adList(int, int)
                 */
                accountingActionsListener.adList(page, limit);
                accountingViewFrame.refreshComplete();
            }

        });
        // the following are default settings
        accountingViewFrame.setResistance(1.7f);
        accountingViewFrame.setRatioOfHeaderHeightToRefresh(1.2f);
        accountingViewFrame.setDurationToClose(200);
        accountingViewFrame.setDurationToCloseHeader(1000);
        // default is false
        accountingViewFrame.setPullToRefresh(false);
        // default is true
        accountingViewFrame.setKeepHeaderWhenRefresh(true);
        accountingAdapter = new AccountingAdapter(accountingBeens/*, this*/);
        accountingAdapter.setActivity(getActivity());
        accountingRecyclerView.setHasFixedSize(true);//固定大小
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        accountingRecyclerView.setLayoutManager(linearLayoutManager);
        //间隔线条
//        heSuanRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        accountingRecyclerView.setAdapter(accountingAdapter);
        isPrepared = true;
        accountingActionsListener.adList(page, limit);
        KLog.e("Evd", " isVisible - " + isVisible);
        lazyLoad();//这里我们调用以下去加载我们的数据
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //3.
        KLog.e("TAG", "onViewCreated=I");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        KLog.i("Evd", "onDestroy");
    }

    //标志位，标志已经初始化完成。
    private boolean isPrepared;

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        //网络请求加载....
        KLog.i("Evd", "lazyLoad");
        /**
         * @see AccountingPresenter#adList(int, int)
         */
        accountingActionsListener.adList(page, limit);


    }


    @Override
    public void showSuccess(List<AccountingBean> infos, String all_total, String deal_total) {
        if (more) {
            accountingBeens.addAll(infos);
            accountingAdapter.addList(accountingBeens);
        } else {
            accountingBeens.clear();
            AccountingBean accountingBean = new AccountingBean();
            accountingBean.setLayoutType(0);
            accountingBean.setAll_total(all_total);
            accountingBean.setDeal_total(deal_total);
            accountingBeens.add(0, accountingBean);
            accountingBeens.addAll(infos);
            accountingAdapter.addList(accountingBeens);
        }

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void dismissProgress() {


    }

    @Override
    public void showMessage(String message) {
        EasyToast.showShort(getActivity(), message);
    }

    @Override
    public Context getContext() {
        return getActivity();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
